<?php

class Subcategory extends CActiveRecord {

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'subcategories';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('name', 'required'),
            array('id, category_id, name, cover_image, slug, active, order, url,file_ext, old_name', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'category_id' => 'Category',
            'name' => 'Name',
            'cover_image' => 'Hình đại diện',
            'slug' => 'Slugs',
            'active' => 'Active',
            'order' => 'Thứ tự hiện thị',
            'url' => 'url',
            'file_ext' => 'file extension',
            'old_name' => 'file old name'
        );
    }

    public function behaviors() {
        return array(
            'sluggable' => array(
                'class' => 'application.extensions.mintao-yii-behavior-sluggable.SluggableBehavior',
                'columns' => array('slug'),
                'unique' => true,
                'update' => true,
            ),
        );
    }

    protected function beforeSave() {
        $this->slug = trim(strtolower(StringHelper::remove_vietnamese_accents($this->name)));
        return parent::beforeSave();
    }

    public function search() {
        // @todo Please modify the following code to remove attributes that should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id, true);
        $criteria->compare('category_id', $this->category_id);
        $criteria->compare('name', $this->name, true);
        $criteria->compare('cover_image', $this->cover_image, true);
        $criteria->compare('slug', $this->slug, true);
        $criteria->compare('active', $this->active);

        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                ));
    }

    /**
     * Returns the static model of the specified AR class.
     * Please note that you should have this exact method in all your CActiveRecord descendants!
     * @param string $className active record class name.
     * @return Subcategory the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

}
